package com.jyx.project.controller;

import com.alibaba.fastjson.JSON;
import com.jyx.common.response.AppResponse;
import com.jyx.common.vo.BaseVo;
import com.jyx.project.pojo.TReturn;
import com.jyx.project.service.ProjectCreateService;
import com.jyx.project.vo.req.ProjectBaseInfoVo;
import com.jyx.project.vo.req.ProjectReturnInfoVo;
import com.jyx.project.vo.req.ProjectStoreToRedisVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "创建项目的模块,一共四步")
@RestController
@RequestMapping("/create")
public class ProjectCreateController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired(required = false)
    private ProjectCreateService projectCreateService;

    @PostMapping("init")
    @ApiOperation("第一步:初始化项目")
    public AppResponse<String> init(BaseVo baseVo){
        //根据用户令牌从redis中获取用户id
        String memberId = redisTemplate.opsForValue().get(baseVo.getAccessToken());
        //判断是否登录
        if (StringUtils.isEmpty(memberId)){
            return AppResponse.fail("登录信息已过期,请重新登录");
        }
        //初始化项目
        String projectToken = projectCreateService.initCreateProject(Integer.parseInt(memberId));
        return AppResponse.ok(projectToken);
    }

    @PostMapping("/addBaseInfo")
    @ApiOperation("第二步:添加项目的基本信息")
    public AppResponse<String> addBaseInfo(ProjectBaseInfoVo baseInfoVo){
        //判断是否登录
        String meberId = redisTemplate.opsForValue().get(baseInfoVo.getAccessToken());
        if (StringUtils.isEmpty(meberId)){
            return AppResponse.fail("请登录");
        }
        //从redis中获取项目信息
        String redisVoStr = redisTemplate.opsForValue().get(baseInfoVo.getProjectToken());
        //字符串转对象
        ProjectStoreToRedisVo redisVo = JSON.parseObject(redisVoStr, ProjectStoreToRedisVo.class);
        //添加项目基本信息
        BeanUtils.copyProperties(baseInfoVo,redisVo);
        //存入到redis中
        String jsonString = JSON.toJSONString(redisVo);
        redisTemplate.opsForValue().set(baseInfoVo.getProjectToken(),jsonString);
        return AppResponse.ok(baseInfoVo.getProjectToken());
    }

    @ApiOperation("第三步:添加项目汇报信息")
    @PostMapping("/addReturnInfo")
    public AppResponse<String> addReturnInfo(@RequestBody List<ProjectReturnInfoVo> returns){
        //判断是否登录
        String meberId = redisTemplate.opsForValue().get(returns.get(0).getAccessToken());
        if (StringUtils.isEmpty(meberId)){
            return AppResponse.fail("请登录");
        }
        //从redis中获取项目信息
        String projectToken = returns.get(0).getProjectToken();
        String redisVoStr = redisTemplate.opsForValue().get(projectToken);
        //将JSON字符串转换为projectStoreToRedisVo
        ProjectStoreToRedisVo projectStoreToRedisVo = JSON.parseObject(redisVoStr, ProjectStoreToRedisVo.class);
        //创建存储TReturn的集合
        ArrayList<TReturn> returnList = new ArrayList<>();
        //将前台传入的数据存入TReturn中
        for (ProjectReturnInfoVo returnInfoVo : returns) {
            TReturn tReturn = new TReturn();
            BeanUtils.copyProperties(returnInfoVo,tReturn);
            returnList.add(tReturn);
        }
        //将TReturn的数据存入projectStoreToRedisVo
        projectStoreToRedisVo.setProjectReturns(returnList);
        //将projectStoreToRedisVo存入到redis中
        String jsonString = JSON.toJSONString(projectStoreToRedisVo);
        redisTemplate.opsForValue().set(returns.get(0).getProjectToken(),jsonString);
        return AppResponse.ok(returns.get(0).getProjectToken());
    }

    @ApiOperation("第四步:添加项目到数据库")
    @PostMapping("/storeToDB")
    public AppResponse<String> storeToDB(String accessToken,String projectToken){
        // 是否登录
        String memberIdStr = redisTemplate.opsForValue().get(accessToken);
        if(StringUtils.isEmpty(memberIdStr)){
            return AppResponse.fail("请登录");
        }
        // 获取redis中的project
        String redisVoStr = redisTemplate.opsForValue().get(projectToken);
        ProjectStoreToRedisVo redisVo = JSON.parseObject(redisVoStr, ProjectStoreToRedisVo.class);
        // 将项目信息写入数据库
        projectCreateService.saveProjectInfo(redisVo);
        return AppResponse.ok("成功");
    }
}
